package com.sskj.hangqing.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.KeyBoardUtil;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.bean.SelfCoinBean;
import com.sskj.hangqing.presenter.SelfCoinAddPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/09/02 17:43:39
 */
@Route(path = RConfig.HANG_ZIXUAN_ADD)
public class SelfCoinAddActivity extends BaseActivity<SelfCoinAddPresenter> {
    @BindView(R2.id.ivBack1)
    ImageView ivBack1;
    @BindView(R2.id.ivRight)
    ImageView ivRight;
    @BindView(R2.id.etSearch)
    EditText etSearch;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;

    private String keywords;

    private MVCCoolHelper<List<SelfCoinBean>> mvcHelper;
    private SlimAdapter slimAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.hang_activity_self_coin_add;
    }

    @Override
    public SelfCoinAddPresenter getPresenter() {
        return new SelfCoinAddPresenter();
    }

    @Override
    public void initView() {
        ivRight.setImageResource(R.mipmap.lib_icon_search);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.hang_adapter_item_self_coin, new SlimInjector<SelfCoinBean>() {
            @Override
            public void onInject(SelfCoinBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tvTitle, data.getCode());
                Switch swAdd = (Switch) injector.findViewById(R.id.swAdd);
                swAdd.setChecked(data.isSelect());
                swAdd.setOnCheckedChangeListener((compoundButton, b) -> mPresenter.edit(data.getId(), b));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        etSearch.setOnEditorActionListener((textView, i, keyEvent) -> {
            if ((keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_SEARCH) || i == EditorInfo.IME_ACTION_SEARCH) {
                KeyBoardUtil.closeKeybord(etSearch, SelfCoinAddActivity.this);
                search();
                return true;
            }
            return false;
        });

        ClickUtil.click(300, ivRight, this::search);
        ClickUtil.click(300, ivBack1, this::finish);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<SelfCoinBean> refreshDataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<SelfCoinBean>) page ->
                        mPresenter.getData(keywords, page), 20
                );
        mvcHelper.setDataSource(refreshDataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.refresh();
    }

    private void search() {
        Log.d("zixuan", "search: " + keywords);
        keywords = etSearch.getText().toString();
        if (TextUtils.isEmpty(keywords))
            return;
        mvcHelper.refresh();
    }
}
